$(function () {

    //执行滑动验证码方法
    verifyFn('270','135','270',function (gapLeft,moveLeft) {
        $("[name='gapLeft']").val(gapLeft);
        $("[name='moveLeft']").val(moveLeft);
    });

    $('body').on('click', '#login', function () { //执行登录
        var _this = $(this);
        _this.attr("disabled","disabled").text('登录中...');

        var passWord = $("input[name=password]").val();
        var _passWord = passWord;
        if(passWord !=null && $.trim(passWord)!=''){
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey(publicKey);
            passWord= encrypt.encrypt(passWord);
            $("input[name=password]").val(passWord);
        }
        var gapLeft= $("input[name=gapLeft]").val();
        var _gapLeft = gapLeft;
        if(gapLeft !=null && $.trim(gapLeft)!=''){
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey(publicKey);
            gapLeft= encrypt.encrypt(gapLeft);
            $("input[name=gapLeft]").val(gapLeft);
        }
        var moveLeft= $("input[name=moveLeft]").val();
        var _moveLeft = moveLeft;
        if(moveLeft !=null && $.trim(moveLeft)!=''){
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey(publicKey);
            moveLeft= encrypt.encrypt(moveLeft);
            $("input[name=moveLeft]").val(moveLeft);
        }

        var params = $('#loginForm').serialize();
        $.ajax({
            url: baseloaction + '/doLogin?sign='+sign,
            data: params,
            dataType: 'json',
            type: 'post',
            success: function (response) {
                if (response.success) {
                    window.location.href = baseloaction + response.entity;
                } else {
                    sign = response.keyMap.sign;
                    publicKey = response.keyMap.publicKey;
                    $("input[name=password]").val(_passWord);
                    $("input[name=moveLeft]").val(_moveLeft);
                    $("input[name=gapLeft]").val(_gapLeft);
                    _this.removeAttr("disabled").text('登录');
                    $('#errorTips').html(response.message).parents('div.alert-warning').show();
                }
            },
            error: function () {
                $("input[name=password]").val(_passWord);
                $("input[name=moveLeft]").val(_moveLeft);
                $("input[name=gapLeft]").val(_gapLeft);
                _this.removeAttr("disabled").text('登录');
                $('#errorTips').html('系统错误，登录失败').parents('div.alert-warning').show();
            }
        });
    }).on('keyup', 'input[name=account], input[name=password], input[name=captcha]', function (event) {
        event = event || window.event;
        if (event && event.keyCode == 13) { //回车事件
            $('#login').click();
        }
    }).on('focus', 'input.validate-data', function () {
        $('div.alert-warning').hide();
    });

    $('.signin-form-bx').on('click', '.input-group-addon', function () {
        $(this).next('.form-control').focus();
    });
    
});


